<template>
  <view
    style="border: 1px solid #dadada; border-radius: 5px; height: 28px; margin-left: 10rpx"
    :class="{
      disabled: disabled
    }"
    :style="{
      backgroundColor: disabled ? '#f5f7fa' : '#fff'
    }"
  >
    <picker style="margin: 3px 5px" @change="pickerChange" v-model="internalValue" :range="rangeList" :range-key="rangeKey" :disabled="disabled">
      <view>
        {{ rangeList.some((item) => item[rangeValue] == internalValue) ? rangeList.find((item) => item[rangeValue] == internalValue)[rangeKey] : placeholder }}
      </view>
    </picker>
  </view>
</template>
<script>
export default {
  name: 'LiPicker',
  props: {
    value: {},
    rangeList: Array,
    rangeKey: {
      type: String,
      default: 'text'
    },
    rangeValue: {
      type: String,
      default: 'id'
    },
    notFoundText: {
      type: String,
      default: '无'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: '无'
    }
  },
  data() {
    return {
      internalValue: this.value
    };
  },
  watch: {
    value(newVal) {
      this.internalValue = newVal;
    }
  },
  methods: {
    pickerChange(e) {
      this.$emit('input', this.rangeList[e.detail.value][this.rangeValue]);
      this.$emit('change', {
        detail: {
          value: this.rangeList[e.detail.value][this.rangeValue]
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.disabled {
  background-color: #f5f7fa;
}
</style>
